package cp

import (
	"fmt"
	"github.com/gin-gonic/gin"
	nhttp "net/http"
	"wzgames/internal/admin/adc"
	"wzgames/internal/admin/adsv/syssv"
	"wzgames/internal/admin/api/mid"
	"wzgames/internal/admin/model"
	"wzgames/internal/response"
	"wzgames/pkg/server/http"
	"wzgames/pkg/server/http/types"
	"wzgames/pkg/utils/stringutil"
)

func RegisterCachePushRoute(pGroup *gin.RouterGroup) {
	bizGroup := pGroup.Group("cp")
	http.MethodMapper(bizGroup, types.POST)(pushSysCurrFn())
	http.MethodMapper(bizGroup, types.POST)(pushSysGameFn())
	http.MethodMapper(bizGroup, types.POST)(pushSysGameCurrFn())
}

var pushSysCurrFn = func() (string, func(c *gin.Context)) {
	uri := fmt.Sprintf("/sys_currency")
	return uri, func(c *gin.Context) {
		adminId := mid.AdminId(c)
		adm := adc.GetReceiver().GetAdmin(adminId)
		if adm == nil {
			c.JSON(nhttp.StatusOK, model.Response{Code: response.ErrUnAuthorize})
			return
		}
		resp := model.Response{}

		code := syssv.SV().PublishCurrToCache(adm)
		resp.Code = code

		c.JSON(nhttp.StatusOK, resp)
		return
	}
}

var pushSysGameFn = func() (string, func(c *gin.Context)) {
	uri := fmt.Sprintf("/sys_game/:gameid")
	return uri, func(c *gin.Context) {
		adminId := mid.AdminId(c)
		adm := adc.GetReceiver().GetAdmin(adminId)
		if adm == nil {
			c.JSON(nhttp.StatusOK, model.Response{Code: response.ErrUnAuthorize})
			return
		}
		id := c.Param("gameid")
		gameId, _ := stringutil.ToInt32(id, 0)
		if gameId <= 0 {
			c.JSON(nhttp.StatusOK, model.Response{Code: response.ErrBadRequest})
			return
		}
		resp := model.Response{}
		code := syssv.SV().PublishSysGameToCache(adm, gameId)
		resp.Code = code

		c.JSON(nhttp.StatusOK, resp)
		return
	}
}

var pushSysGameCurrFn = func() (string, func(c *gin.Context)) {
	uri := fmt.Sprintf("/sys_game_currency/:gcId")
	return uri, func(c *gin.Context) {
		adminId := mid.AdminId(c)
		adm := adc.GetReceiver().GetAdmin(adminId)
		if adm == nil {
			c.JSON(nhttp.StatusOK, model.Response{Code: response.ErrUnAuthorize})
			return
		}
		id := c.Param("gcId")
		gcId, _ := stringutil.ToInt64(id, 0)
		if gcId <= 0 {
			c.JSON(nhttp.StatusOK, model.Response{Code: response.ErrBadRequest})
			return
		}
		resp := model.Response{}
		code := syssv.SV().PublishSysGameCurrToCache(adm, gcId)
		resp.Code = code

		c.JSON(nhttp.StatusOK, resp)
		return
	}
}
